﻿<%@ Page Title="" Language="C#" MasterPageFile="~/AdminCP/Layouts/Admin.Master" AutoEventWireup="true"
    CodeBehind="BonusPayingManager.aspx.cs" Inherits="MLM.AdminCP.Pages.BonusPayingManager" meta:resourcekey="PageResource1" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
    <style type="text/css">
        #Pager { float: right; }
        #Pager select { margin-top: 5px; padding: 2px; border: solid 1px #ccc; width: 80px; }
        #ListUserWithBonusCurrentMonth td { text-align: center; }
        #SetStatusPaid { margin-top: 3px; margin-left: 0; }
    </style>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
    
    <asp:Label runat="server" ID="lbbonuspaying" Text="Bonus Paying Manager" 
        meta:resourcekey="lbbonuspayingResource1"></asp:Label>
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="ContentPlaceHolder2" runat="server">
    <table class="MLMInfoTable" cellpadding="0" cellspacing="0" id="ListUserWithBonusCurrentMonth">
        <tr>
            <th>
            </th>
            <th>
                
                <asp:Label runat="server" ID="lbUserID" Text="User ID" 
                    meta:resourcekey="lbUserIDResource1"></asp:Label>
            </th>
            <th>
                
                <asp:Label runat="server" ID="lbName" Text="Name" 
                    meta:resourcekey="lbNameResource1"></asp:Label>
            </th>
            <th>
                
                <asp:Label runat="server" ID="lbbonusineuro" 
                    Text="Bonus in &euro; for the current month" 
                    meta:resourcekey="lbbonusineuroResource1"></asp:Label>
            </th>
            <th>
                
                <asp:Label runat="server" ID="lbStatus" Text="Status" 
                    meta:resourcekey="lbStatusResource1"></asp:Label>
            </th>
            <th>
                Print/Create Bonus paper
                <asp:Label runat="server" ID="lbPrintCreateBonuspaper" 
                    Text="Print/Create Bonus paper" 
                    meta:resourcekey="lbPrintCreateBonuspaperResource1"></asp:Label>
            </th>
        </tr>
        <tr>
            <th>
            </th>
            <th>
                
                <asp:Label runat="server" ID="lbuserid2" Text="User ID" 
                    meta:resourcekey="lbuserid2Resource1"></asp:Label>
            </th>
            <th>
                
                <asp:Label runat="server" ID="lbname2" Text="Name" 
                    meta:resourcekey="lbname2Resource1"></asp:Label>
            </th>
            <th>
                
                <asp:Label runat="server" ID="lbbonusineuro2" 
                    Text="Bonus in &euro; for the current month" 
                    meta:resourcekey="lbbonusineuro2Resource1"></asp:Label>
            </th>
            <th>
                
                <asp:Label runat="server" ID="lbStatus2" Text="Status" 
                    meta:resourcekey="lbStatus2Resource1"></asp:Label>
            </th>
            <th>
                
                <asp:Label runat="server" ID="lbprintcreate" Text="Print/Create Bonus paper" 
                    meta:resourcekey="lbprintcreateResource1"></asp:Label>
            </th>
        </tr>
    </table>
    <p>
        <div id="Pager" class="Pager">
        </div>
        <span id="SetStatusPaid" class="button" onclick="SetPaidStatus();">Set status to 'Bonus
            Paid' of selected Users </span>
    </p>
    <script type="text/javascript">
        var CurrentPageIndex = 1;
        var PageSize = 10;
        function CalculateBonusThisMonth() {
            $.ajax({
                type: "POST",
                url: "/AdminCP/Pages/BonusPayingManager.aspx/CalculateBonusThisMonth",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                beforeSend: function () {
                    $('body').append('<div id="loadingDiv" style="position:fixed; top:50%; left:50%; width:200px;text-align:center; padding:3px; border:solid 1px #ccc; background:#f1f1f1;">Calculating, please wait...</div>');
                },
                data: "{'_pageIndex':'" + CurrentPageIndex + "','_pageSize':'" + PageSize + "'}",
                async: true,
                success: function (msj) {
                    $('#loadingDiv').remove();
                    var data = msj.d;
                    if (typeof (data) == "string") {
                        data = JSON.parse(data);
                    }
                    $('.BonusStatusItem').remove();
                    $.each(data.Data, function (i, item) {
                        var chk = '<input type="checkbox" userid="' + item.UserId + '"/>';
                        if (parseInt(item.PaidStatus) == 0) {
                            item.PaidStatus = 'Pending';
                        } else {
                            item.PaidStatus = 'Bonus Paid';
                            chk = 'Paid';
                        }
                        var queryDetail = '?UserId=' + item.UserId + '&Name=' + item.Name + '&Status=' + item.PaidStatus + '&Bonus=' + item.CurrentBonus;
                        $('#ListUserWithBonusCurrentMonth tr:first').after('<tr class="BonusStatusItem" userid="' + item.UserId + '"><td>' + chk + '</td><td>' + item.UserId + '</td><td>' + item.Name + '</td><td>' + item.CurrentBonus + '</td><td>' + item.PaidStatus + '</td><td><a href="/AdminCP/Pages/BonusPayingDetail.aspx' + queryDetail + '">Process...</a></td></tr>');
                    });
                    WrapPaging(data.TotalRow, CurrentPageIndex, PageSize, '#Pager', function (p, n) {
                        CurrentPageIndex = p;
                        CalculateBonusThisMonth();
                    });
                }
            });
        }
        $(document).ajaxStop(function () {
            $('#loadingDiv').remove();
        });
        function WrapPaging(numRow, PageIndex, PageSize, objContainer, callBack) {
            var btnNext = $(objContainer).attr("id") + "_btnNextPage";
            var btnPrev = $(objContainer).attr("id") + "_btnPrevPage";
            var selectboxPage = $(objContainer).attr("id") + "_pageListOption";
            var sizePage = $(objContainer).attr("id") + "_sizeListOption";
            var numPage = numRow / PageSize;

            if (PageSize >= numRow) PageIndex = 1;

            if (numPage > Math.floor(numPage))
                numPage = Math.floor(numRow / PageSize) + 1;

            if (PageIndex > numPage) PageIndex = 1;

            var sHTML = "";

            var NaviButton = "<div class=\"fr\"><a href=\"javascript:void(0)\" class=\"cms_bm_news_btback\" id=\"" + btnPrev + "\"></a><a href=\"javascript:void(0)\" class=\"cms_bm_news_btgo\" id=\"" + btnNext + "\"></a></div>";
            var IndexTo = PageIndex * PageSize;
            var IndexForm = IndexTo - PageSize + 1;
            if (IndexTo > numRow) IndexTo = numRow;
            sHTML += "<select id=\"" + selectboxPage + "\">";
            for (var i = 1; i <= numPage; i++) {
                var iTo = i * PageSize;
                var iFrom = iTo - PageSize + 1;
                if (iTo > numRow) iTo = numRow;
                sHTML += "<option value=\"" + i + "\" " + (i == PageIndex ? "selected=\"selected\"" : "") + ">" + iFrom + '-' + iTo + "</option>";
            }
            sHTML += "</select>";
            $(objContainer).html(sHTML + NaviButton);
            $("#" + selectboxPage).change(function () { callBack(this.value, PageSize); });
            var _prevIndex = PageIndex <= 1 ? _prevIndex = 1 : PageIndex - 1;
            var _nexIndex = PageIndex >= numPage ? _nexIndex = numPage : parseInt(parseInt(PageIndex) + 1);
            if (PageIndex <= 1) {
                $("#" + btnPrev).addClass("disabled");
                $("#" + btnPrev).unbind("click");
            }
            else {
                $("#" + btnPrev).removeClass("disabled");
                $("#" + btnPrev).unbind("click").click(function () { callBack(_prevIndex, PageSize); });
            }
            if (PageIndex >= numPage) {
                $("#" + btnNext).addClass("disabled");
                $("#" + btnNext).unbind("click");
            }
            else {
                $("#" + btnNext).removeClass("disabled");
                $("#" + btnNext).unbind("click").click(function () { callBack(_nexIndex, PageSize); });
            }
        }
        function SetPaidStatus() {
            var listOfUserId = "";
            $('.BonusStatusItem input:checked').each(function (i, item) {
                listOfUserId += $(this).attr('userid') + ";";
            });
            if (listOfUserId != '') {
                $.ajax({
                    type: "POST",
                    url: "/AdminCP/Pages/BonusPayingManager.aspx/SetPaidStatus",
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    beforeSend: function () {
                        $('body').append('<div id="loadingDiv" style="position:fixed; top:50%; left:50%; width:200px;text-align:center; padding:3px; border:solid 1px #ccc; background:#f1f1f1;">Calculating, please wait...</div>');
                    },
                    data: "{'listOfUserId':'" + listOfUserId + "'}",
                    async: true,
                    success: function (msj) {
                        CalculateBonusThisMonth();
                    }
                });
            }
        }
        $(function () {
            CalculateBonusThisMonth();
        });
    </script>
</asp:Content>
